home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / capturing / simplevideoout / simplevideoout.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  11.8 KB  |  368 lines

  1. /*
  2.     File:        SimpleVideoOut.c
  3.     
  4.     Description: SimpleVideoOut is an example of using QuickTimes FireWire video
  5.                  output component to play a DV stream (.dv movie) out to a DV Camera.
  6.                  This code is based on VidOutApp originally written by Jay Lloyd, Casey King
  7.                  and Adrienne Wang.
  8.  
  9.     Author:        era
  10.  
  11.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  12.     
  13.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  14.                 ("Apple") in consideration of your agreement to the following terms, and your
  15.                 use, installation, modification or redistribution of this Apple software
  16.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  17.                 please do not use, install, modify or redistribute this Apple software.
  18.  
  19.                 In consideration of your agreement to abide by the following terms, and subject
  20.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  21.                 copyrights in this original Apple software (the "Apple Software"), to use,
  22.                 reproduce, modify and redistribute the Apple Software, with or without
  23.                 modifications, in source and/or binary forms; provided that if you redistribute
  24.                 the Apple Software in its entirety and without modifications, you must retain
  25.                 this notice and the following text and disclaimers in all such redistributions of
  26.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  27.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  28.                 Apple Software without specific prior written permission from Apple.  Except as
  29.                 expressly stated in this notice, no other rights or licenses, express or implied,
  30.                 are granted by Apple herein, including but not limited to any patent rights that
  31.                 may be infringed by your derivative works or by other works in which the Apple
  32.                 Software may be incorporated.
  33.  
  34.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  35.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  36.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  38.                 COMBINATION WITH YOUR PRODUCTS.
  39.  
  40.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  41.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  42.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  44.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  45.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  46.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47.                 
  48.     Change History (most recent first): <1> 1/28/00 initial release
  49.  
  50. */
  51.  
  52. #include <Movies.h>
  53. #include <QuickTimeComponents.h>
  54. #include <MacWindows.h>
  55. #include <Gestalt.h>
  56. #include <ToolUtils.h>
  57.  
  58. #include "CVideoOutput.h"
  59.  
  60. using namespace dts;
  61.  
  62. const short kAlert            = 128;
  63. const short kPopUpMenu        = 129;
  64. const short kEchoOnID        = 1;
  65. const short kEchoOffID        = 2;
  66. const short kVOSoundOnID    = 4;
  67. const short kVOSoundOffID    = 5;
  68.  
  69. // Globals
  70. static WindowRef        gWindow = NULL;
  71. static Movie             gMovie = NULL;
  72. static MovieController    gController = NULL;
  73. static MenuHandle         gPopUpMenuHandle = NULL;
  74. static short            gMCHeight = 0;
  75.  
  76. static Boolean            gDone = false;
  77.  
  78. void main( void );
  79. void Initialize( void );
  80. OSErr DoOpenMovieFromFile( FSSpec *inFSSpec );
  81. OSErr DoCreateMovieController( CVideoOutput *inVideoOutputPtr );
  82. static pascal Boolean myMCActionFilterWithRefConProc( MovieController theMC, short theAction, void *theParams, long theRefCon );
  83.  
  84. void Initialize( void )
  85. {
  86.     long    result;
  87.     
  88.     InitGraf( &qd.thePort );
  89.     InitFonts();
  90.     InitWindows();
  91.     InitMenus();
  92.     TEInit();
  93.     InitDialogs( nil );
  94.     InitCursor();
  95.     FlushEvents( everyEvent, 0 );
  96.     
  97.     // Check for QuickTime
  98.     if ( Gestalt( gestaltQuickTime, &result ) != noErr) {
  99.         ParamText( "\pQuickTime is not installed...", NULL, NULL, NULL );
  100.         StopAlert( kAlert, NULL );
  101.         ExitToShell();                    
  102.     }
  103.     
  104.     // Initialize all the needed managers
  105.     MaxApplZone();
  106.     for ( UInt8 i = 0; i < 4; i++ ) {
  107.         MoreMasters();
  108.     }
  109.  
  110.     EnterMovies();
  111.     
  112.     // Setup default menu options
  113.     gPopUpMenuHandle = GetMenu( kPopUpMenu );
  114.     if ( gPopUpMenuHandle == NULL ) {
  115.         ParamText( "\pCould not load menu resource...", NULL, NULL, NULL );
  116.         StopAlert( kAlert, NULL );
  117.         ExitToShell();                    
  118.     }
  119.     
  120.     InsertMenu( gPopUpMenuHandle, -1 );
  121.     CheckItem( gPopUpMenuHandle, kEchoOnID, true );
  122.     CheckItem( gPopUpMenuHandle, kEchoOffID, false );
  123.     CheckItem( gPopUpMenuHandle, kVOSoundOnID, true );
  124.     CheckItem( gPopUpMenuHandle, kVOSoundOffID, false );
  125. }
  126.  
  127. OSErr DoOpenMovieFromFile( FSSpec *inFSSpec ) 
  128. {
  129.     Rect    theWindowRect = { 50, 20, 300, 300 };
  130.     Rect    theMovieBox;
  131.     short    theMovieRefNum;
  132.     
  133.     OSErr    rc = noErr;
  134.     
  135.     // Open the movie file and create our window but don't show it yet
  136.     rc = OpenMovieFile( inFSSpec, &theMovieRefNum, fsRdPerm );
  137.     if ( rc ) return rc;
  138.  
  139.     rc = NewMovieFromFile( &gMovie, theMovieRefNum, NULL, NULL, newMovieActive, NULL );    
  140.     
  141.     CloseMovieFile( theMovieRefNum );
  142.     if ( rc ) return rc;
  143.     
  144.     gWindow = NewCWindow( nil, &theWindowRect, inFSSpec->name, false, kWindowFloatProc, (WindowPtr)-1, true, 0 );
  145.     if ( gWindow == NULL ) return -1;
  146.                 
  147.     SetPort( gWindow );
  148.  
  149.     GetMovieBox( gMovie, &theMovieBox );
  150.     OffsetRect( &theMovieBox, -theMovieBox.left, -theMovieBox.top );
  151.     SetMovieBox( gMovie, &theMovieBox );
  152.     
  153.     SetMovieGWorld( gMovie, (CGrafPtr)gWindow, NULL );
  154.     
  155.     return rc;
  156. }
  157.  
  158. OSErr DoCreateMovieController( CVideoOutput *inVideoOutputPtr )
  159. {
  160.     Rect    theMovieBox;
  161.     long     mcFlags;
  162.     
  163.     OSErr    rc = noErr;
  164.     
  165.     // Create the movie controller
  166.     GetMovieBox( gMovie, &theMovieBox );
  167.     gMCHeight = theMovieBox.bottom;
  168.     
  169.     gController = NewMovieController( gMovie, &theMovieBox, 0L | mcTopLeftMovie );
  170.     if ( gController == nil) return -1;
  171.     
  172.     // Enable keys and give us a custom button
  173.     MCDoAction( gController, mcActionSetKeysEnabled, (Ptr)true );
  174.     MCDoAction( gController, mcActionGetFlags, &mcFlags );
  175.     MCDoAction( gController, mcActionSetFlags, (Ptr)( mcFlags | mcFlagsUseCustomButton ));
  176.     
  177.     rc = MCGetControllerBoundsRect( gController, &theMovieBox );
  178.     if ( rc ) return rc;
  179.     gMCHeight = theMovieBox.bottom - gMCHeight;
  180.     
  181.     // Resize and show the move window which now contains the attached controller
  182.     SizeWindow( gWindow, theMovieBox.right, theMovieBox.bottom, true );
  183.     ShowWindow( gWindow );
  184.     
  185.     // Install an action filter for application-specific mc action processing
  186.     // This is where the CustomButton actions are handled, we pass in a pointer to
  187.     // our VideoOutput object so we can change it's settings as the custom options are chosen
  188.     MCSetActionFilterWithRefCon( gController, NewMCActionFilterWithRefConProc(myMCActionFilterWithRefConProc), (long)inVideoOutputPtr );
  189.     
  190.     return rc;
  191. }
  192.  
  193. static pascal Boolean myMCActionFilterWithRefConProc( MovieController theMC, short theAction, void *theParams, long theRefCon )
  194. {
  195. #pragma unused( theParams )
  196.  
  197.     Boolean    isHandled = false;
  198.     
  199.     CVideoOutput *pVideoOutput = (CVideoOutput *)theRefCon;
  200.     if ( pVideoOutput == NULL )
  201.         return isHandled;
  202.         
  203.     switch ( theAction ) {    
  204.     case mcActionCustomButtonClick:
  205.         Rect theWindowBoundsRect, theMCBoundsRect;
  206.         
  207.         // Calculate the position for the CustomButton pop-up menu
  208.         RgnHandle theRgn = NewRgn();
  209.             GetWindowStructureRgn( gWindow, theRgn );
  210.             theWindowBoundsRect = (**theRgn).rgnBBox;
  211.         DisposeRgn( theRgn );
  212.         
  213.         MCGetControllerBoundsRect( theMC, &theMCBoundsRect );
  214.         LocalToGlobal( (Point *)&theMCBoundsRect.bottom );    
  215.         
  216.         // Handle the menu selections and settings
  217.         long theResult = PopUpMenuSelect( gPopUpMenuHandle, theMCBoundsRect.bottom - gMCHeight, theWindowBoundsRect.right + 1, 0 );
  218.         short theItem = LoWord( theResult );
  219.         
  220.         switch ( theItem ) {
  221.         case kEchoOnID:
  222.             CheckItem( gPopUpMenuHandle, kEchoOnID, true );
  223.             CheckItem ( gPopUpMenuHandle, kEchoOffID, false );
  224.             pVideoOutput->SetEchoPort( (CGrafPtr)gWindow );
  225.             break;
  226.         case kEchoOffID:
  227.             CheckItem( gPopUpMenuHandle, kEchoOnID, false );
  228.             CheckItem ( gPopUpMenuHandle, kEchoOffID, true );
  229.             pVideoOutput->SetEchoPort();
  230.             break;            
  231.         case kVOSoundOnID:
  232.             CheckItem( gPopUpMenuHandle, kVOSoundOnID, true );
  233.             CheckItem( gPopUpMenuHandle, kVOSoundOffID, false );
  234.             pVideoOutput->SetSoundDevice();
  235.             break;            
  236.         case kVOSoundOffID:
  237.             CheckItem( gPopUpMenuHandle, kVOSoundOnID, false );
  238.             CheckItem( gPopUpMenuHandle, kVOSoundOffID, true );
  239.             pVideoOutput->SetSoundDevice( false );
  240.             break;
  241.             
  242.         default:
  243.             break;
  244.         } // switch
  245.             
  246.         isHandled = true;
  247.         break;
  248.             
  249.     default:
  250.         break;
  251.             
  252.     } // switch
  253.     
  254.     return isHandled;    
  255. }
  256.  
  257. void main( void )
  258. {
  259.     EventRecord         theEvent;
  260.     WindowRef            theFoundWindow;
  261.     short                theWindowPart;
  262.     ComponentResult     isEventHandled;    
  263.     SFTypeList            theTypeList = { MovieFileType, 0, 0, 0 };
  264.     StandardFileReply    theReply;
  265.     
  266.     OSErr                rc = noErr;
  267.         
  268.     CVideoOutput        *pVideoOutput = NULL;
  269.  
  270.     Initialize();
  271.     
  272.     StandardGetFilePreview( nil, 1, theTypeList, &theReply );
  273.     if ( !theReply.sfGood ) goto bail;
  274.     
  275.     rc = DoOpenMovieFromFile( &theReply.sfFile );
  276.     if ( rc ) { ParamText( "\pProblem opening movie from file...", NULL, NULL, NULL ); goto bail; }
  277.     
  278.     // Instantiate a VideoOutput object
  279.     pVideoOutput = new CVideoOutput( gMovie );
  280.     if ( pVideoOutput == NULL ) rc = memFullErr;
  281.     if ( rc || ( rc = pVideoOutput->GetError() ) != noErr ) { ParamText( "\pProblem initializing CVideoOutput, or FireWire component unavailable...", NULL, NULL, NULL ); goto bail; }
  282.     
  283.     rc = DoCreateMovieController( pVideoOutput );
  284.     if ( rc ) { ParamText( "\pProblem creating movie controller.", NULL, NULL, NULL ); goto bail; }
  285.     
  286.     // Open the component - pass in the client name and the display mode, for our purposes 
  287.     // we're going with the default mode '1' which is the ever popular 'NTSC'
  288.     rc = pVideoOutput->Open( "\pSimpleVideoOut", 1 );
  289.     if ( rc ) { ParamText( "\pUnable to sucessfully open the video output component...", NULL, NULL, NULL ); goto bail; }
  290.     
  291.     // Begin the video output, both sound and clock options are set to true in the interface by default
  292.     rc = pVideoOutput->Begin();
  293.     if ( rc ) { ParamText( "\pAttempting to get exclusive access to hardware failed...", NULL, NULL, NULL ); goto bail; }
  294.     
  295.     // The echo port is our main window
  296.     pVideoOutput->SetEchoPort( (CGrafPtr)gWindow );
  297.     
  298.     while (!gDone ) {        
  299.  
  300.         if ( WaitNextEvent( everyEvent, &theEvent, 5, NULL ) ) {
  301.  
  302.             if ( gController )
  303.                 isEventHandled = MCIsPlayerEvent( gController, &theEvent );
  304.  
  305.             if ( !isEventHandled ) {
  306.                 switch ( theEvent.what ) {                        
  307.                 case mouseDown:
  308.                     theWindowPart = FindWindow( theEvent.where, &theFoundWindow );
  309.                     
  310.                     switch ( theWindowPart ) {
  311.                     case inDrag:
  312.                         DragAlignedWindow( theFoundWindow, theEvent.where, &qd.screenBits.bounds, NULL, NULL );
  313.                         break;
  314.                     case inGoAway:
  315.                         gDone = TrackGoAway( theFoundWindow, theEvent.where );
  316.                         pVideoOutput->End();
  317.                         break;
  318.                     default:
  319.                         break;
  320.                     } // switch                
  321.                     break;
  322.                 case updateEvt:
  323.                     //    Handle update events for window.
  324.                     theFoundWindow = (WindowPtr)theEvent.message;
  325.                     if ( theFoundWindow == gWindow ) {
  326.                         BeginUpdate( theFoundWindow );        
  327.                             UpdateMovie( gMovie );
  328.                             SetPort( theFoundWindow );
  329.                             EraseRect( &theFoundWindow->portRect );
  330.                             MCDraw( gController, gWindow );
  331.                         EndUpdate( theFoundWindow );
  332.                     }
  333.                     break;                                
  334.                 case osEvt:
  335.                     if ( ( theEvent.message & ( suspendResumeMessage << 24 )) != 0 ) {
  336.                         if ( ( theEvent.message & resumeFlag ) != 0 ) {
  337.                             MCDraw( gController, gWindow );
  338.                         }
  339.                     }
  340.                     break;
  341.                 
  342.                 default:
  343.                     break;
  344.                 } // switch
  345.             }
  346.         } else {
  347.             MCIdle( gController );
  348.         }
  349.     } // while
  350.         
  351. bail:
  352.     if ( rc ) StopAlert( kAlert, NULL );
  353.     
  354.     if ( pVideoOutput ) {
  355.         delete pVideoOutput;
  356.     }
  357.     
  358.     if ( gController != NULL )
  359.         DisposeMovieController( gController );
  360.     
  361.     if ( gMovie != NULL )
  362.         DisposeMovie( gMovie );
  363.     
  364.     if ( gWindow != NULL )
  365.         DisposeWindow( gWindow );
  366.         
  367.     ExitMovies();
  368. }